********************************************************************************
***																			 ***
***     				  PERU - WOMEN COVID19 PROJECT 						 ***
***						   PILOT FIRST ROUND SURVEY					         ***
***																			 ***
********************************************************************************


/*** Defines global
	 ***************************************************************************/
	
	dis "`c(username)'"
	

// 	if "`c(username)'" == "PMatos" {
// 	global user "C:/Users/PMatos/Dropbox"
// 	global user_box "C:/Users/PMatos/Box Sync"

// 	global data_folder   "$user/HRI - MIMP/01_ProjectDevelopment/03 Covid Activities/04. Women Survey/03_Survey/Data Management/00 HFC/05_data/02_survey"
// 	global hfc_folder    "$user/HRI - MIMP/01_ProjectDevelopment/03 Covid Activities/04. Women Survey/03_Survey/Data Management/00 HFC"
// 	global outcomes_bartik  "$hfc_folder/02_dofiles/04 Shift_Share/outcomes"
// 	global inputs_bartik    "$hfc_folder/02_dofiles/04 Shift_Share/inputs"
// 	global enaho 		 "C:/Data/ENAHO/2019"
// 	global enaho20 "C:/Data/ENAHO/2020"


// 	 }

cap program drop addSectorLabels
program define addSectorLabels
	** Sectores Completos a CIIU Rev.4
	* Actividad económica
	gen sector1r4=.
	replace sector1r4=1 if p506r4>=100 & p506r4<300
	replace sector1r4=2 if p506r4>=300 & p506r4<500
	replace sector1r4=3 if p506r4>=500 & p506r4<1000
	replace sector1r4=4 if p506r4>=1000 & p506r4<3500
	replace sector1r4=5 if p506r4>=3500 & p506r4<3600
	replace sector1r4=6 if p506r4>=3600 & p506r4<4100
	replace sector1r4=7 if p506r4>=4100 & p506r4<4500
	replace sector1r4=8 if p506r4>=4500 & p506r4<4900
	replace sector1r4=9 if p506r4>=4900 & p506r4<5500
	replace sector1r4=10 if p506r4>=5500 & p506r4<5800
	replace sector1r4=11 if p506r4>=5800 & p506r4<6400
	replace sector1r4=12 if p506r4>=6400 & p506r4<6800
	replace sector1r4=13 if p506r4>=6800 & p506r4<6900
	replace sector1r4=14 if p506r4>=6900 & p506r4<7700
	replace sector1r4=15 if p506r4>=7700 & p506r4<8400
	replace sector1r4=16 if p506r4>=8400 & p506r4<8500
	replace sector1r4=17 if p506r4>=8500 & p506r4<8600
	replace sector1r4=18 if p506r4>=8600 & p506r4<9000
	replace sector1r4=19 if p506r4>=9000 & p506r4<9400
	replace sector1r4=20 if p506r4>=9400 & p506r4<9700
	replace sector1r4=21 if p506r4>=9700 & p506r4<9900
	replace sector1r4=22 if p506r4==9900 

	#delimit ;
	lab def sector1r4 
	1 "Agricultura, ganadería y silvicultura" 
	2 "Pesca y acuicultura" 
	3 "Explotación de minas y canteras" 
	4 "Industrias manufactureras" 
	5 "Suministro de electricidad, gas, vapor y aire acondicionado"
	6 "Suministro de agua; evacuación de aguas residuales, gestión de desechos y descontaminación" 
	7 "Construcción" 
	8 "Comercio al por mayor y al por menor; reparación de vehículos automotores y motocicletas" 
	9 "Transporte y almacenamiento" 
	10 "Actividades de alojamiento y de servicio de comidas" 
	11 "Información y comunicaciones" 
	12 "Actividades financieras y de seguros" 
	13 "Actividades inmobiliarias"
	14 "Actividades profesionales, científicas y técnicas" 
	15 "Actividades de servicios administrativos y de apoyo" 
	16 "Administración pública y defensa; planes de seguridad social de afiliación obligatoria " 
	17 "Enseñanza" 
	18 "Actividades de atención de la salud humana y de asistencia social" 
	19 "Actividades artísticas, de entretenimiento y recreativas" 
	20 "Otras actividades de servicios"
	21 "Actividades de los hogares como empleadores; actividades no diferenciadas de los hogares como productores de bienes y servicios para uso propio"
	22 "Actividades de organizaciones y órganos extraterritoriales"; 
	#delimit cr
	lab val sector1r4 sector1r4

end
**************************************************************************
************************ENAHO*********************************************
**************************************************************************

use "$enahoData/enaho_2019-Modulo05/enaho01a-2019-500.dta", clear
addSectorLabels

***** 
keep ubigeo ocu500 fac500a sector1r4 p507 p523 p524a1 p530a p538e1 conglome estrato
keep if ocu500==1

***Nivel dpto
gen dpto=substr(ubigeo,1,2)
order dpto


***Checking
svyset [pweight = fac500a ], psu(conglome) strata(estrato)
svy: tab sector1r4 ocu500 , count format(%10.0g)

***Making it in code
**1) National level
collapse (count) N = fac500a (sum) weighted_N_national =  fac500a ,by(sector1r4)
gen year=2019
gen realYear = 2019
gen quarter = .
drop N
rename weighted_N_national weighted_N_national_19
gen weighted_N_national = weighted_N_national_19
save "$data_folder/sector_enaho2019.dta", replace

**************************************************************************
************************ ENAHO 2019 quarterly  *********************************************
**************************************************************************
use "$enahoData/enaho_2019-Modulo05/enaho01a-2019-500.dta", clear
addSectorLabels
destring mes, replace
gen _date = mdy(mes,1,2019)
gen quarter = quarter(_date)
***** 
keep ubigeo ocu500 fac500a sector1r4 p507 p523 p524a1 p530a p538e1 conglome estrato quarter
keep if ocu500==1

***Nivel dpto
gen dpto=substr(ubigeo,1,2)
order dpto

gen year=2019
gen realYear = 2019


***Making it in code
**1) National level
collapse (count) N = fac500a (sum) weighted_N_national =  fac500a ,by(year realYear sector1r4 quarter)

rename weighted_N_national weighted_N_national_19
gen weighted_N_national = weighted_N_national_19
save "$data_folder/sector_enaho2019_quarterly.dta", replace



**************************************************************************
************************ENAHO*********************************************
**************************************************************************

use "$enahoData/enaho_2020_tri1-Modulo05/enaho01a_2020_500.dta", clear
addSectorLabels

***** 
keep ubigeo ocu500 fac500 sector1r4 p507 p523 p524a1 p530a conglome estrato
keep if ocu500==1

***Nivel dpto
gen dpto=substr(ubigeo,1,2)
order dpto

***Checking
svyset [pweight = fac500 ], psu(conglome) strata(estrato)
svy: tab sector1r4 ocu500 , count format(%10.0g)

***Making it in code
**1) National level
collapse (count) N = fac500 (sum) weighted_N_national =  fac500 ,by(sector1r4)

rename weighted_N_national weighted_N_national_20
gen weighted_N_national= weighted_N_national_20
gen realYear = 2020
gen quarter = 1 

save "$data_folder/sector_enaho2020q1.dta", replace



use "$enahoData/enaho_2020_tri2-Modulo05/enaho01a_2020_500.dta", clear
addSectorLabels

***** 
keep ubigeo ocu500 fac500 sector1r4 p507 p523 p524a1 p530a conglome estrato
keep if ocu500==1

***Nivel dpto
gen dpto=substr(ubigeo,1,2)
order dpto

***Checking
svyset [pweight = fac500 ], psu(conglome) strata(estrato)
svy: tab sector1r4 ocu500 , count format(%10.0g)

***Making it in code
**1) National level
collapse (count) N = fac500 (sum) weighted_N_national =  fac500 ,by(sector1r4)
gen year=2020
gen realYear = 2020
gen quarter = 2

rename weighted_N_national weighted_N_national_20
gen weighted_N_national= weighted_N_national_20

save "$data_folder/sector_enaho2020q2.dta", replace

****

use "$enahoData/enaho_2020_tri3-Modulo05/enaho01a_2020_500.dta", clear
addSectorLabels

***** 
keep ubigeo ocu500 fac500 sector1r4 p507 p523 p524a1 p530a conglome estrato
keep if ocu500==1

***Nivel dpto
gen dpto=substr(ubigeo,1,2)
order dpto

***Checking
svyset [pweight = fac500 ], psu(conglome) strata(estrato)
svy: tab sector1r4 ocu500 , count format(%10.0g)

***Making it in code
**1) National level
collapse (count) N = fac500 (sum) weighted_N_national =  fac500 ,by(sector1r4)
gen year=2021
gen realYear = 2020
gen quarter = 3

rename weighted_N_national weighted_N_national_21
gen weighted_N_national= weighted_N_national_21

save "$data_folder/sector_enaho2020q3.dta", replace

**************************************************************************
************************ENAHO*********************************************
**************************************************************************
use "$data_folder/sector_enaho2019.dta", clear
append using  "$data_folder/sector_enaho2020q2.dta"
append using  "$data_folder/sector_enaho2020q3.dta"

// gen weighted_N_national=weighted_N_national_19 if year==2019
// replace weighted_N_national=weighted_N_national_20 if year==2020
// replace weighted_N_national=weighted_N_national_21 if year==2021


keep sector1r4 year weighted_N_national
sort sector1r4 year
duplicates drop sector1r4 year, force

save "$data_folder/sector_enaho2020_2019.dta", replace



**************************************************************************
** 2019 year average and 2020 Full quarterly panel
**************************************************************************
use "$data_folder/sector_enaho2019.dta", clear
append using  "$data_folder/sector_enaho2020q1.dta"
append using  "$data_folder/sector_enaho2020q2.dta"
append using  "$data_folder/sector_enaho2020q3.dta"

save "$data_folder/sector_enaho_2019_all2020Quarters.dta", replace


**************************************************************************
** 2019 all quarter and 2020 Full quarterly panel
**************************************************************************
use "$data_folder/sector_enaho2019_quarterly.dta", clear
append using  "$data_folder/sector_enaho2020q1.dta"
append using  "$data_folder/sector_enaho2020q2.dta"
append using  "$data_folder/sector_enaho2020q3.dta"

save "$data_folder/sector_enaho_2019allquarter_all2020Quarters.dta", replace


